package cn.com.controller.operationController;


import cn.com.Enum.SessionAttr;
import cn.com.Enum.StoreStatus;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Store;
import cn.com.db.service.OrderService;
import cn.com.db.service.StoreService;
import cn.com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Time;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/store")
public class StoreOperationController {

    @Autowired
    OrderService orderService;
    @Autowired
    StoreService storeService;
    @RequestMapping(path = "/create",method = RequestMethod.POST)
    public String createStore(HttpServletRequest request,
                              Store store)
    {
        DataResult dataResult=new DataResult();
        String sellerAuthingId=(String) SessionUtil.getAttr(request, SessionAttr.SELLERAUTHINGID);
        if (store.getStart().after(store.getEnd())){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("开始营业时间必须在结束营业时间之前");
            return JsonUtil.toJsonString(dataResult);
        }
        store.setId(UidUtil.createUUID());
        store.setSeller(sellerAuthingId);
        store.setStatus(StoreStatus.RUNNING.getStatus());
        store.setCreated_at(TimeUtil.getNowTimestamp());
        store.setUpdated_at(TimeUtil.getNowTimestamp());
        storeService.createStore(store);
        store = storeService.modifyEchoTime(store);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("新增成功");
        dataResult.setData(store);
        return JsonUtil.toJsonString(dataResult);
    }
    @RequestMapping(method = RequestMethod.POST)
    public String getStore(HttpServletRequest request,
                           @RequestParam("status")Integer status,
                           @RequestParam("page")Integer page,
                           @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult(page,count);
        String sellerAuthingId=(String) SessionUtil.getAttr(request, SessionAttr.SELLERAUTHINGID);
        Store store=new Store();
        store.setSeller(sellerAuthingId);
        store.setDel(false);
        store.setStatus(status);
        List<Store> stores = storeService.getStore(store,page,count);
        stores = storeService.modifyEchoTime(stores);
        
        dataResult.setData(stores);
        dataResult.setTotal(storeService.countGetStore(store));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }


//    @RequestMapping(value = "/delete",method = RequestMethod.POST)
//    public String deleteStore(@RequestParam("storeId")String storeId)
//    {
//        DataResult dataResult=new DataResult();
//        storeService.deleteStore(storeId);
//        dataResult.setCode(Code.SUCCESS);
//        dataResult.setMessage("删除成功");
//        return JsonUtil.toJsonString(dataResult);
//    }

//    @RequestMapping(value = "/verify",method = RequestMethod.POST)
//    public String verify(@RequestParam("storeId")String storeId){
//        DataResult dataResult=new DataResult();
//        dataResult.setCode(Code.SUCCESS);
//        dataResult.setMessage("操作成功");
//        return JsonUtil.toJsonString(dataResult);
//    }


    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updateStore(HttpServletRequest request,
                              Store store){
        DataResult dataResult=new DataResult();
        String sellerAuthingId=(String) SessionUtil.getAttr(request, SessionAttr.SELLERAUTHINGID);
        Store s=storeService.getStoreById(store.getId());
        if (s==null||s.getDel()){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("店铺不存在,或店铺已被删除");
            return JsonUtil.toJsonString(dataResult);
        }
        storeService.updateStore(store);
        Store store1 = storeService.getStoreById(s.getId());  
        // 需要加上八小时,否则前端显示出错
        store1 = storeService.modifyEchoTime(store1);
        dataResult.setData(store1);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String deleteStore(HttpServletRequest request,
                              @RequestParam("storeId")String id){
        DataResult dataResult=new DataResult();
        Store s=storeService.getStoreById(id);
        if (s==null||s.getDel()){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("店铺不存在,或店铺已被删除");
            return JsonUtil.toJsonString(dataResult);
        }
        storeService.deleteStore(id);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("删除成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/onOrOff",method = RequestMethod.POST)
    public String onOrOff(HttpServletRequest request,
                              @RequestParam("storeId")String id){
        DataResult dataResult=new DataResult();
        Store s=storeService.getStoreById(id);
        if (s==null||s.getDel()){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("店铺不存在,或店铺已被删除");
            return JsonUtil.toJsonString(dataResult);
        }
        if (s.getStatus().equals(StoreStatus.RUNNING.getStatus())){
            storeService.updateStoreStatus(id,StoreStatus.BREAK.getStatus());
        }else if (s.getStatus().equals(StoreStatus.BREAK.getStatus())){
            storeService.updateStoreStatus(id,StoreStatus.RUNNING.getStatus());
        }
        Store store = storeService.getStoreById(s.getId());
        // 加八小时
        storeService.modifyEchoTime(store);
        dataResult.setData(store);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/getTurnover",method = RequestMethod.POST)
    public String getTurnover(@RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(orderService.getTurnover(storeId,null));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/getTodayTurnover",method = RequestMethod.POST)
    public String getTodayTurnover(@RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(orderService.getTodayTurnover(storeId,null));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getOrderNum",method = RequestMethod.POST)
    public String getOrderNum(@RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(orderService.getOrderNum(storeId,null));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getDealPeopleNum",method = RequestMethod.POST)
    public String getDealPeopleNum(String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(orderService.getDealPeopleNum(storeId,null));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }
}
